﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Our.Umbraco.uDbCompare
{
    public partial class LegacyAlias : System.Web.UI.UserControl
    {
        public bool UseRemoteConnectionString { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindData();
        }

        private string connectionString
        {
            get
            {
                string connectionString = uDbCompare.CurrentConnectionString;
                if (UseRemoteConnectionString)
                    connectionString = uDbCompare.RemoteConnectionString;
                return connectionString;
            }
        }


        public void BindData()
        {
            if (uDbCompare.IsValidDbConnection(UseRemoteConnectionString))
            {
                ShowLegacyButton.Enabled = true;
            }
        }

        protected void ShowLegacyButton_OnClick(object sender, EventArgs e)
        {
            string legacyQuery = @"select distinct
                                        case when nodeObjectType = 'A2CB7800-F571-4787-9638-BC48539A0EFB' then 'DocumentType' when nodeObjectType = '9B5416FB-E72F-45A9-A07B-5A9A2709CE43' then 'MemberType' when nodeObjectType = '4EA4382B-2F5A-4C2B-9587-AE9B3CF3602E' then 'MediaType' else 'Other' end as 'Category'
                                        , alias as 'ContentTypeAlias' ,'' as 'PropertyAlias'
                                        from cmsContentType ct join umbracoNode n on ct.nodeid = n.id
                                        where alias like '% %'
                                        union
                                        select distinct 
                                        case when nodeObjectType = 'A2CB7800-F571-4787-9638-BC48539A0EFB' then 'DocumentType' when nodeObjectType = '9B5416FB-E72F-45A9-A07B-5A9A2709CE43' then 'MemberType' when nodeObjectType = '4EA4382B-2F5A-4C2B-9587-AE9B3CF3602E' then 'MediaType' else 'Other' end as 'Category'
                                        , ct.alias as 'ContentTypeAlias' ,pt.alias as 'PropertyAlias'
                                        from cmsPropertyType pt join cmsContentType ct on pt.contentTypeId = ct.NodeId
                                         join umbracoNode n on ct.nodeid = n.id
                                        where pt.alias like '% %'
                                            ";
                DataTable legacyTable = uDbCompare.QueryDb(legacyQuery, connectionString);
                legacyTable.DefaultView.Sort = "Category asc, ContentTypeAlias asc, PropertyAlias asc";

                LegacyGridView.DataSource = legacyTable;
                LegacyGridView.DataBind();

                NoResultsLabel.Visible = (legacyTable.Rows.Count == 0);
        }

    }
}